package com.yunxi.service.dubboService.impl;

import com.github.pagehelper.PageInfo;
import com.yunxi.job.BaseJob;
import com.yunxi.model.quartz.JobAndTrigger;
import com.yunxi.quartz.pojo.JobAndTriggerRespVO;
import com.yunxi.quartz.service.JobAndTriggerDubboService;
import com.yunxi.service.localService.JobAndTriggerService;
import org.quartz.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenmeng on 2019/5/21.
 */
//@Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class JobAndTriggerDubboServiceImpl implements JobAndTriggerDubboService {

    //加入Qulifier注解，通过名称注入bean
    @Autowired
    @Qualifier("Scheduler")
    private Scheduler scheduler;

    @Autowired
    @Qualifier(value = "jobAndTriggerServiceImpl")
    private JobAndTriggerService jobAndTriggerServiceImpl;

    @Override
    public PageInfo<JobAndTriggerRespVO> getJobAndTriggerDetails(int pageNum, int pageSize) {
        List<JobAndTrigger> jobAndTriggers = jobAndTriggerServiceImpl.getJobAndTriggerDetails(pageNum,pageSize);
        List<JobAndTriggerRespVO> resp = new ArrayList<>();
        for(JobAndTrigger jobAndTrigger : jobAndTriggers){
             JobAndTriggerRespVO vo = new JobAndTriggerRespVO();
             BeanUtils.copyProperties(jobAndTrigger,vo);
             resp.add(vo);
        }
        PageInfo<JobAndTriggerRespVO> pageInfo = new PageInfo<>(resp);
        return pageInfo;
    }

    /**
     * 添加任务
     * @param jobClassName
     * @param jobGroupName
     * @param cronExpression
     * @throws Exception
     */
    public void addJob(String jobClassName, String jobGroupName, String cronExpression)throws Exception{

        // 启动调度器
        scheduler.start();

        //构建job信息
        JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName).getClass()).withIdentity(jobClassName, jobGroupName).build();

        //表达式调度构建器(即任务执行的时间)
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

        //按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
                .withSchedule(scheduleBuilder).build();

        try {
            scheduler.scheduleJob(jobDetail, trigger);

        } catch (SchedulerException e) {
            System.out.println("创建定时任务失败"+e);
            throw new Exception("创建定时任务失败");
        }
    }

    public void jobPause(String jobClassName, String jobGroupName) throws Exception {
        scheduler.pauseJob(JobKey.jobKey(jobClassName, jobGroupName));
    }

    public void jobresume(String jobClassName, String jobGroupName) throws Exception{
        scheduler.resumeJob(JobKey.jobKey(jobClassName, jobGroupName));
    }

    public void jobreschedule(String jobClassName, String jobGroupName, String cronExpression) throws Exception {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
            // 表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

            // 按新的cronExpression表达式重新构建trigger
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();

            // 按新的trigger重新设置job执行
            scheduler.rescheduleJob(triggerKey, trigger);
        } catch (SchedulerException e) {
            System.out.println("更新定时任务失败"+e);
            throw new Exception("更新定时任务失败");
        }
    }

    public void jobdelete(String jobClassName, String jobGroupName) throws Exception {
        scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
        scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
        scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
    }

    public static BaseJob getClass(String classname) throws Exception
    {
        Class<?> class1 = Class.forName(classname);
        return (BaseJob)class1.newInstance();
    }
}
